package com.ensibs.nuitinfo2013;

import java.util.ArrayList;
import java.util.List;

import com.ensibs.nuitinfo2013.model.Collection;
import com.ensibs.nuitinfo2013.model.Topic;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FermerTopicActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fermer_topic);
		
		Bundle objetbunble = this.getIntent().getExtras();
		Topic topic = (Topic) objetbunble.get("topic");
		topic = Collection.getInstance().getTopicById(topic.getId());
		Log.i("ni", (topic == null)+"");
		TextView title = (TextView) findViewById(R.id.textViewConsulation);
		if(topic.isResolu())
			title.setText(topic.getTitre()+"\n"+getString(R.string.topic_close));
		else {
			Log.i("NULL", (title == null)+"");
			title.setText(topic.getTitre()+"\n"+getString(R.string.topic_open));
		}
		ListView list =  (ListView) findViewById(R.id.listViewConsulation);
		updateList(list, topic);
	}
	
	
	public void closeTopic(View view) {
		Bundle objetbunble = this.getIntent().getExtras();
		Topic topic = (Topic) objetbunble.get("topic");
		topic = Collection.getInstance().getTopicById(topic.getId());
		Collection.getInstance().closeTopic(topic.getId());
		finish();
	}
	
	
	private void updateList(ListView list, Topic topic){
		List<String> data = topic.getReponses();
		data = new ArrayList<String>(data);
		data.add(0,topic.getQuestion());
		ArrayAdapter<String> adapateur = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
		list.setAdapter(adapateur);
	}
}
